package browserUI
{
	import browserUI.assets.KeyGraphics.back;
	import browserUI.assets.KeyGraphics.close;
	import browserUI.assets.KeyGraphics.enter;
	import browserUI.assets.KeyGraphics.open;
	import browserUI.assets.KeyGraphics.shift;
	
	import dev.touchswipe.shared.Animation.Animation;
	import dev.touchswipe.shared.Fonts;
	import dev.touchswipe.shared.utils.WAVDecoder;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.SampleDataEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.filters.DropShadowFilter;
	import flash.media.Sound;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.utils.ByteArray;
	
	public class Key extends Sprite
	{
		private var _text:TextField;
		private var _textFormat:TextFormat;
		private var _graphic:DisplayObject;
		private var _anim:Animation;
		
		public static const SPACE:String="_SPACE_";
		public static const CLOSE:String="_CLOSE_";
		public static const OPEN:String="_OPEN_";
		public static const BACK:String="_BACK_";
		public static const SHIFT:String="_SHIFT_";
		public static const ENTER:String="_ENTER_";
		public static const NUMBER_MODE:String="?123";
		public static const LETTER_MODE:String="ABC";
		
		private static var file:File;
		private static var fileStream:FileStream=new FileStream();
		private static var fileData:ByteArray=new ByteArray();
		private static var wavDecoder:WAVDecoder=new WAVDecoder();
		private static var sound:Sound=new Sound();
		
		public function Key(letter:String)
		{
			super();
			
			if(!fileData.length){
				file=File.applicationDirectory.resolvePath("key.wav");
				fileStream.open(file,FileMode.READ);
				fileStream.readBytes(fileData);
				fileStream.close();
				wavDecoder.decode(fileData);
			}
			
			mouseChildren=false;
			
			filters=[new DropShadowFilter(1,90,0,0.7,2,2)];
			
			_textFormat=new TextFormat(Fonts.MyriadPro,20,0x333333);
			_textFormat.align=TextFormatAlign.CENTER;
			
			_anim=Animation.makeCallbackAnimation(callBack,this,1,0,5);
			
			switch (letter){
				case SPACE:
					break;
				case CLOSE:
					_graphic=new close();
					break;
				case OPEN:
					_graphic=new open();
					break;
				case SHIFT:
					_graphic=new shift();
					break;
				case BACK:
					_graphic=new back();
					break;
				case ENTER:
					_graphic=new enter();
					break;
				default:
					_text=new TextField();
					_text.embedFonts=true;
					_text.text=letter;
					_text.selectable=false;
			}
			_id=letter;
			
			redraw();
			if(_graphic)
				addChild(_graphic);
			if(_text)
				addChild(_text);
			addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				sound=new Sound();
				sound.addEventListener(SampleDataEvent.SAMPLE_DATA,wavDecoder.writeSampleData);
				sound.play();
				_anim.start();
			});
		}
		
		private var _id:String;
		
		public function get id():String
		{
			return _id;
		}
		
		private var _width:Number=80;
		
		public override function get width():Number
		{
			return _width;
		}
		
		private function callBack(thisObj:Object,value:Number):void{
			var dR:int=value*((0xFF0000&0x99CCFF)-(0xFF0000&0xE7E6EB))>>16;
			var dG:int=value*((0x00FF00&0x99CCFF)-(0x00FF00&0xE7E6EB))>>8;
			var dB:int=value*((0x0000FF&0x99CCFF)-(0x0000FF&0xE7E6EB));
			var bgCol:uint=(0xE7E6EB+(dR<<16)+(dG<<8)+dB);
			graphics.clear();
			graphics.beginFill(_toggleState?0x99CCFF:bgCol);
			graphics.drawRoundRect(0,0,_width,_height,14);
		}
		
		public override function set width(value:Number):void
		{
			_width = value;
			if(_id==SPACE)
				_width=4*value;
			if(_id==NUMBER_MODE||_id==ENTER||_id==LETTER_MODE)
				_width=1.5*value;
			redraw();
		}
		
		
		private var _height:Number=80;
		
		public override function get height():Number
		{
			return _height;
		}
		
		public override function set height(value:Number):void
		{
			_height = value;
			redraw();
		}
		
		
		private var _toggleState:Boolean;

		public function get toggleState():Boolean
		{
			return _toggleState;
		}

		public function set toggleState(value:Boolean):void
		{
			_toggleState = value;
			redraw();
		}
		
		
		private function redraw():void{
			graphics.clear();
			graphics.beginFill(_toggleState?0x99CCFF:0xE7E6EB);
			graphics.drawRoundRect(0,0,_width,_height,14);
			
			if(_graphic){
				var size:Number=Math.min(_width,_height);
				_graphic.height=0.8*size;
				_graphic.width=_graphic.height;
				_graphic.x=(_width-_graphic.width)/2;
				_graphic.y=(_height-_graphic.height)/2;
			}
			
			if(_text){
				_textFormat.size=_height/3;
				
				_text.setTextFormat(_textFormat);
				_text.width=_width;
				_text.y=_height/4;
				_text.height=3*height/4;
			}
		}
	}
}